package game.services.session.handler;

import com.google.protobuf.GeneratedMessageV3;
import dou.net.socket.IClientSocket;
import game.socket.AbstractMessageHandler;
import game.socket.ClientSocketManager;
import game.socket.RemoteClientSocket;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import protocol.Session;

import java.nio.ByteBuffer;

public class SessionSendLoginHandler extends AbstractMessageHandler
{
    private static Logger logger = LogManager.getLogger("GameServer");

    @Override
    public void messageHandler(int msgID, int msgNum, GeneratedMessageV3 message, IClientSocket socket) throws Exception
    {
        Session.Req_SessionLogin loginInfo = (Session.Req_SessionLogin)message;

        // 记录客户端
        ClientSocketManager.getInstance().addClient(loginInfo.getUid(), (RemoteClientSocket) socket);
        logger.info("user: " + loginInfo.getUid() + " connected!");

        // 请求返回
        Session.Recv_SessionLogin.Builder recvBuilder = Session.Recv_SessionLogin.newBuilder();
        Session.Recv_SessionLogin recvInfo = recvBuilder.build();
        ByteBuffer recvBytes = encodeMessage(msgID, msgNum, recvInfo, false);
        socket.send(recvBytes);
    }
}
